export default class Bus {
  constructor () {
    this.event = {}
    this.onlyEvent = {}
  }
  on (type, fn) {
    if (!this.event[type]) {
      this.event[type] = []
    }
    this.event[type].push(fn)
  }
  emit (type, ...args) {
    if (this.event[type]) {
      this.event[type].forEach(fn => fn(...args))
    }
    // console.log('这里执行了嘛', this.onlyEvent[type])
    if (this.onlyEvent[type]) {
      Object.values(this.onlyEvent[type]).forEach(fn => fn(...args))
    }
  }
  off (type, fn) {
    if (this.event[type]) {
      const index = this.event[type].findIndex(f => fn === f)
      this.event[type].splice(index, 1)
    }
  }
  offOnly (type, id) {
    if (this.onlyEvent[type]) {
      delete this.onlyEvent[type][id]
    }
  }
  only (type, id, fn) {
    if (!this.onlyEvent[type]) {
      this.onlyEvent[type] = {}
    }
    if (id) {
      this.onlyEvent[type][id] = fn
    }
  }
}